package com.cqjybigscreen.utils;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author Rao Chang
 * @version 1.0
 * 时间处理工具类
 */
public class TimesUtils {
    /**
     * 获取一个月前的开始时间
     */
    public static String getLastMonthStartTime(LocalDate today) {

        // 计算30天前的日期
        LocalDate thirtyDaysAgo = today.minusDays(30);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return thirtyDaysAgo.format(formatter);
    }

    /**
     * 获取3月前的开始时间
     */
    public static String getLastThreeMonthStartTime(LocalDate today) {

        // 计算30天前的日期
        LocalDate thirtyDaysAgo = today.minusDays(90);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return thirtyDaysAgo.format(formatter);
    }

    /**
     * 获取最近一个月每一天的LocalDate类型的日期
     */
    public static List<LocalDate> getLastMonthDays(LocalDate today) {
        List<LocalDate> dates = new ArrayList<>();
        LocalDate startDate = today.minusDays(30);

        for (int i = 0; i < 30; i++) {
            dates.add(startDate);
            startDate = startDate.plusDays(1);
        }
        return dates;
    }

    /***
     * 获取最近3个月(90)天的日期
     */
    public static List<LocalDate> getLastThreeMonthDays(LocalDate today) {
        List<LocalDate> dates = new ArrayList<>();
        LocalDate startDate = today.minusDays(90);

        for (int i = 0; i < 90; i++) {
            dates.add(startDate);
            startDate = startDate.plusDays(1);
        }
        return dates;
    }

    /**
     * 获取最近12个月的日期
     */
    public static List<LocalDate> getRecent12Months(LocalDate today) {
        List<LocalDate> months = new ArrayList<>();
        LocalDate currentMonth = today.with(TemporalAdjusters.firstDayOfMonth());

        for (int i = 0; i < 12; i++) {
            months.add(0, currentMonth);
            currentMonth = currentMonth.minusMonths(1);
        }

        return months;
    }

    /**
     * 前端要求的时间以月份/日期的字符串格式返回
     */
    public static String getDateString(LocalDate date, Boolean b) {
        int year = date.getYear();
        int month = date.getMonth().getValue();
        int day = date.getDayOfMonth();
        return b ? month + "/" + day : year + "/" + month;
    }

    /**
     * 去掉后缀无效0
     */
    public static String removeTrailingZeros(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }
        int decimalIndex = input.indexOf('.');
        if (decimalIndex == -1) {
            return input;
        }
        String integerPart = input.substring(0, decimalIndex);
        String decimalPart = input.substring(decimalIndex + 1);
        int lastNonZeroIndex = decimalPart.length() - 1;
        while (lastNonZeroIndex >= 0 && decimalPart.charAt(lastNonZeroIndex) == '0') {
            lastNonZeroIndex--;
        }
        if (lastNonZeroIndex < 0) {
            return integerPart;
        }
        return integerPart + "." + decimalPart.substring(0, lastNonZeroIndex + 1);
    }
    /**
     * 获取最近半年的开始时间
     */
    public static String getLastHalfYearStartTime(LocalDate today,long month) {
        LocalDate thirtyDaysAgo = today.minusMonths(month);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return thirtyDaysAgo.format(formatter);

    }

    /**
     * 返回四个参数,分别是:同比近半年的开始时间,同比近半年的结束时间.环比近半年的开始时间,环比近半年的结束时间
     * @param now
     * @return
     */
    public static List<LocalDate> calculatePeriodDates(LocalDate now) {
        // 同比近半年
        ArrayList<LocalDate> localDates = new ArrayList<>();
        LocalDate yearAgo = now.minusYears(1);
        LocalDate halfYearAgoYearOnYear = getHalfYearAgo(yearAgo);
        localDates.add(halfYearAgoYearOnYear);
        localDates.add(yearAgo);

        // 环比近半年
        LocalDate oneMonthAgo = now.minusMonths(1);
        LocalDate halfYearAgoMonthOnMonth = getHalfYearAgo(oneMonthAgo);
        LocalDate monthAgo = now.minusMonths(1);
        localDates.add(halfYearAgoMonthOnMonth);
        localDates.add(monthAgo);
        return localDates;
    }

    /**
     *
     * 工具转换不用管
     */
    private static LocalDate getHalfYearAgo(LocalDate referenceDate) {
        return referenceDate.minus(Period.ofMonths(5));
    }
}
